// pages/article/article.js
var app = getApp();
var ikz = require('../../Common/tools.js');
Page({
  data: {
    currentTab: 0, //预设当前项的值
    scrollLeft: 20, //tab标题的滚动条位置
    head_info: '',//所有头条信息
    page: 1,//页数
    limit: 6,//每页条数
    tid: 0,//选中分类id
    prodis: 'none',//底部提示是否显示
    showView: false, //发表弹窗
    inputValue: '',//弹窗的值
    bigclose: '',
    advert_info: '',//广告信息
    isFold: false,
    chid: '',//评论时本条消息id
    mid: '',//所评论的评论id
    isallzan: 2,//2可以操作点赞，3不可以，防止重复点击
    iscol: 2,//2可以操作收藏，3不可以，防止重复点击
    remarkshow: 'none', //不回复的情况下 点击遮罩层消失
    imgnum: 1, //banner第几张
    bg_today: '#0AC4BA', //今日信息背景色
    co_today: '', //今日信息字体色
    bg_yesterday: '', //昨日信息背景色
    co_yesterday: '', //昨日信息字体色
  },
  //选择分类
  chooseType: function (e) {
    var that = this;
    var bg_today = '#fff';
    var co_today = '#000';
    var bg_yesterday = 'linear-gradient(-135deg, #0AC4BA 0%, #2BDA8E 100%)';
    var co_yesterday = '#fff';
    that.setData({
      bg_today: bg_today, //今日信息背景色
      co_today: co_today, //今日信息字体色
      bg_yesterday: bg_yesterday, //昨日信息背景色
      co_yesterday: co_yesterday, //昨日信息字体色
    })
  },

  //跳转头条发布
  toHead: function (e) {
    var tid = e.currentTarget.dataset.tid;
    var data = {
      tid: tid,
    }
    ikz.nav('../../Release/headinfo/headinfo', 'n', data);

  },
  //广告图片页
  toImg: function (e) {
    var id = e.currentTarget.dataset.id;
    var typ = 'ad';
    var com = 'home';
    var data = {
      id: id,
      typ: typ,
      com: com,
    }
    ikz.nav('../../Home/images/images', 'n', data);
  },

  // 溢出隐藏样式
  flodFn: function () {
    console.log('改变样式')
    this.setData({
      isFold: !this.isFold
    });
  },

  // 点击收藏
  collectTop: function (e) {
    var hid = e.currentTarget.dataset.hid;
    var that = this;
    var head_info = that.data.head_info;
    //防止重复点击
    that.setData({
      iscol: 3,
    });
    var data = {
      hid: hid,
    };
    var path = 'Index/collectHead';
    ikz.request(path, data, true, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        wx.showToast({
          title: '收藏成功！',
          duration: 2000
        });
        for (var i = 0; i < head_info.length; i++) {
          if (hid == head_info[i].head_id) {
            head_info[i].iscollect = 'yi';
          }
        }
        that.setData({
          head_info: head_info,
        });
      } else if (res.data.status == 'fails') {
        wx.showToast({
          title: '已收藏！',
          duration: 2000
        });
        for (var i = 0; i < head_info.length; i++) {
          if (hid == head_info[i].head_id) {
            head_info[i].iscollect = 'yi';
          }
        }
        that.setData({
          head_info: head_info,
        });
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        });
      }
      that.setData({
        iscol: 2,
      });
    });
  },
  // 取消收藏
  collectTops: function (e) {
    var hid = e.currentTarget.dataset.hid;
    var that = this;
    var head_info = that.data.head_info;
    //防止重复点击
    that.setData({
      iscol: 3,
    });
    var data = {
      hid: hid,
    };
    var path = 'Index/collectCancle';
    ikz.request(path, data, true, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        wx.showToast({
          title: '取消收藏成功！',
          duration: 2000
        });
        for (var i = 0; i < head_info.length; i++) {
          if (hid == head_info[i].head_id) {
            head_info[i].iscollect = 'wei';
          }
        }
        that.setData({
          head_info: head_info,
        });
      } else if (res.data.status == 'fails') {
        wx.showToast({
          title: '已取消收藏！',
          duration: 2000
        });
        for (var i = 0; i < head_info.length; i++) {
          if (hid == head_info[i].head_id) {
            head_info[i].iscollect = 'wei';
          }
        }
        that.setData({
          head_info: head_info,
        });
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        });
      }
      that.setData({
        iscol: 2,
      });
    });
  },
  //本条消息点赞
  goHeadLike: function (e) {
    var hid = e.currentTarget.dataset.hid;
    var that = this;
    var head_info = that.data.head_info;
    //防止重复点击
    that.setData({
      isallzan: 3,
    });
    var data = {
      hid: hid,
    };
    var path = 'Index/goHeadLike';
    ikz.request(path, data, false, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        for (var i = 0; i < head_info.length; i++) {
          if (hid == head_info[i].head_id) {
            head_info[i].iszan = 3;
            head_info[i].head_like = parseInt(head_info[i].head_like) + 1;
          }
        }
        that.setData({
          head_info: head_info,
        });
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        })
      }
      that.setData({
        isallzan: 2,
      });
    });
  },
  //本条取消消息点赞
  noHeadLike: function (e) {
    var hid = e.currentTarget.dataset.hid;
    var that = this;
    var head_info = that.data.head_info;
    //防止重复点击
    that.setData({
      isallzan: 3,
    });
    var data = {
      hid: hid,
    };
    var path = 'Index/noHeadLike';
    ikz.request(path, data, false, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        for (var i = 0; i < head_info.length; i++) {
          if (hid == head_info[i].head_id) {
            head_info[i].iszan = 2;
            head_info[i].head_like = parseInt(head_info[i].head_like) - 1;
          }
        }
        that.setData({
          head_info: head_info,
        });
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        })
      }
      that.setData({
        isallzan: 2,
      });
    });
  },
  //一级评论点赞
  goComLike: function (e) {
    var hid = e.currentTarget.dataset.hid;
    var comid = e.currentTarget.dataset.mid;
    var that = this;
    var head_info = that.data.head_info;
    //防止重复点击
    that.setData({
      isallzan: 3,
    });
    var data = {
      comid: comid,
    };
    var path = 'Index/goComLike';
    ikz.request(path, data, false, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        for (var i = 0; i < head_info.length; i++) {
          if (hid == head_info[i].head_id) {
            for (var k = 0; k < head_info[i].comment_info.length; k++) {
              if (comid == head_info[i].comment_info[k].com_id) {
                head_info[i].comment_info[k].com_like = parseInt(head_info[i].comment_info[k].com_like) + 1;
                head_info[i].comment_info[k].iszan = 3;
              }
            }
          }
        }
        that.setData({
          head_info: head_info,
        });
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        })
      }
      that.setData({
        isallzan: 2,
      });
    });
  },
  //一级评论取消点赞
  noComLike: function (e) {
    var hid = e.currentTarget.dataset.hid;
    var comid = e.currentTarget.dataset.mid;
    var that = this;
    var head_info = that.data.head_info;
    //防止重复点击
    that.setData({
      isallzan: 3,
    });
    var data = {
      comid: comid,
    };
    var path = 'Index/noComLike';
    ikz.request(path, data, false, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        for (var i = 0; i < head_info.length; i++) {
          if (hid == head_info[i].head_id) {
            for (var k = 0; k < head_info[i].comment_info.length; k++) {
              if (comid == head_info[i].comment_info[k].com_id) {
                head_info[i].comment_info[k].com_like = parseInt(head_info[i].comment_info[k].com_like) - 1;
                head_info[i].comment_info[k].iszan = 2;
              }
            }
          }
        }
        that.setData({
          head_info: head_info,
        });
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        })
      }
      that.setData({
        isallzan: 2,
      });
    });
  },

  // 点击查看更多评论
  clickmore: function (e) {
    var hid = e.currentTarget.dataset.hid;
    var that = this;
    var head_info = that.data.head_info;
    var data = {
      hid: hid,
    };
    var path = 'Index/clickMore';
    ikz.request(path, data, false, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        for (var i = 0; i < head_info.length; i++) {
          if (hid == head_info[i].head_id) {
            head_info[i].comment_info = res.data.comment_info;
            head_info[i].ismore = 3;
          }
        }
        that.setData({
          head_info: head_info,
        });
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        })
      }
    });
  },
  // 点击收回更多评论
  clickless: function (e) {
    var hid = e.currentTarget.dataset.hid;
    var that = this;
    var head_info = that.data.head_info;
    for (var i = 0; i < head_info.length; i++) {
      if (hid == head_info[i].head_id) {
        if (head_info[i].comment_info.length > 2) {
          head_info[i].comment_info.length = 2;
        }
        if (head_info[i].comment_info.length > 0) {
          for (var k = 0; k < head_info[i].comment_info.length; k++) {
            head_info[i].comment_info[k].com_son_info = [];
          }
        }

        head_info[i].ismore = 2;
      }
    }
    that.setData({
      head_info: head_info,
    });

  },

  // 点击回复按钮
  reply: function (e) {
    console.log(e);
    var chid = e.currentTarget.dataset.chid;
    var mid = e.currentTarget.dataset.mid;
    var that = this;
    that.setData({
      showView: (!that.data.showView),
      chid: chid,
      mid: mid,
      remarkshow: 'block'
    })
  },
  catchtabinput: function () {
    var that = this;
    that.setData({
      remarkshow: 'block'
    })
  },
  // 不回复 点击关闭
  close: function () {
    var that = this;
    that.setData({
      remarkshow: 'none'
    })
  },
  //获取评论输入框内容
  bindKeyInput: function (e) {
    var that = this;
    this.setData({
      inputValue: e.detail.value,
    })
  },


  // 点击发表
  clickfabiao: function () {
    var that = this;
    var head_info = that.data.head_info;
    var chid = that.data.chid;
    var mid = that.data.mid;
    var com_content = that.data.inputValue;
    //判断评论不能为空
    if (com_content == '') {
      wx.showToast({
        title: '请填写评论',
        duration: 2000
      });
      return false;
    }
    var data = {
      hid: chid,
      mid: mid,
      com_content: com_content,
    }
    var path = 'Index/makeComment';
    ikz.request(path, data, true, (res) => {
      console.log(res.data);

      if (res.data.status == 'success') {
        var old_comment_info = res.data.comment_info;
        for (var i = 0; i < head_info.length; i++) {
          if (chid == head_info[i].head_id) {
            head_info[i].count_comment = parseInt(head_info[i].count_comment) + 1;
            head_info[i].comment_info = old_comment_info;
          }
        }
        wx.showToast({
          title: '发表成功！',
          duration: 2000
        });
        that.setData({
          head_info: head_info,
          showView: (!that.data.showView),
          inputValue: '',
          remarkshow: 'none'
        })
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        })
      }

    });
  },

  // 点赞
  prise: function () {
    wx.showToast({
      title: '点赞成功',
    })
  },

  // 分类滑块
  swichNav: function (e) {
    var cur = e.target.dataset.current;
    var that = this;
    var page = 1;
    var limit = that.data.limit;
    var path = 'Index/index';
    var data = {
      page: page,
      limit: limit,
      tid: cur,
    }
    if (app.globalData.openUserInfo == '') {
      var islogin = false;
    } else {
      var islogin = true;
    }
    ikz.request(path, data, islogin, (res) => {

      if (res.data.status == 'success') {
        that.setData({
          head_info: res.data.head_info,//所有头条信息
          tid: cur,
          page: page,
        })
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        })
      }

    });

    // 设置要显示的数据

    var index = e.target.dataset.index;
    var info = [];
    for (var i = 0; i < that.data.storageinfo.length; i++) {

      if (cur == that.data.storageinfo[i].type_id) {
        info.push(that.data.storageinfo[i])
      } else if (cur == 0) {
        info = that.data.storageinfo;
      }
    }

    that.setData({
      currentinfo: info,
      index: index
    })
    if (that.data.currentTab == cur) { return false; }
    else {
      that.setData({
        currentTab: cur
      })
    }
    that.checkCor();

  },
  //   判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function () {
    if (this.data.index >= 4) {
      this.setData({
        scrollLeft: this.data.index * 100
      })
    } else if (this.data.index < 4) {
      this.setData({
        scrollLeft: 0
      })
    }
  },

  // onLoad: function (options) {
  //   ikz.over(this);
  //   // showView: (options.showView == "true" ? true : false)
  //   // 页面初始化 options为页面跳转所带来的参数
  //   // 页面显示
  //   var that = this;
  //   var bg_today = 'linear-gradient(-135deg, #0AC4BA 0%, #2BDA8E 100%)';
  //   var co_today = '#fff';
  //   var bg_yesterday = '#fff';
  //   var co_yesterday = '#000';
  //   var page = 1;
  //   var limit = that.data.limit;
  //   var path = 'Index/index';
  //   var tid = 0;
  //   var data = {
  //     page: page,
  //     limit: limit,
  //     tid: tid,
  //   }
  //   console.log(app.globalData.openUserInfo);
  //   console.log(43434);
  //   if (app.globalData.openUserInfo == '') {
  //     var islogin = false;
  //   } else {
  //     var islogin = true;
  //   }
  //   ikz.request(path, data, islogin, (res) => {
  //     if (res.data.status == 'success') {
  //       var typeid = [];
  //       for (var i = 0; i < res.data.head_type_info.length; i++) {
  //         typeid[i] = res.data.head_type_info[i].id;
  //       }

  //       res.data.head_type_info.unshift({ head_type_id: 0, head_type_name: '推荐' });//给其分类前面添加一个 全部
  //       that.setData({
  //         head_info: res.data.head_info,//所有头条信息
  //         advert_info: res.data.advert_info,//所有广告信息
  //         storageinfo: res.data.head_type_info, //存储数据
  //         currentinfo: res.data.head_type_info, //显示数据
  //         types: res.data.head_type_info,//全部分类
  //         bg_today: bg_today, //今日信息背景色
  //         co_today: co_today, //今日信息字体色
  //         bg_yesterday: bg_yesterday, //昨日信息背景色
  //         co_yesterday: co_yesterday, //昨日信息字体色

  //         type_id: typeid,
  //         tid: tid,
  //         page: page,
  //         remarkshow: 'none'
  //       });
  //       ikz.hide(that);
  //     } else {
  //       wx.showModal({
  //         title: '提示',
  //         content: '服务器繁忙，请稍后重试！',
  //         showCancel: false
  //       })
  //     }

  //   });
  // },

  // 详情页
  toContent: function (e) {
    var that = this;
    var hid = e.currentTarget.dataset.hid;
    wx.navigateTo({
      url: '../content/content?' + 'hid=' + hid,
    })
  },



  toTop: function () {
    wx.pageScrollTo({
      scrollTop: 0
    })
  },

  onReady: function () {

  },


  //数字转换
  changeNum(num) {
    if (num > 0 && num < 10000) {
      num = num / 100;
      num = Math.ceil(num) / 10;
      var newnum = num + 'k';
    } else if (num >= 10000) {
      num = num / 1000;
      num = Math.ceil(num) / 10;
      var newnum = num + 'w';
    } else {
      var newnum = 0;
    }
    return newnum;
  },

  onShow: function () {


  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },

  onPullDownRefresh: function () {
    wx.showLoading({
      title: '刷新中...',
    });
    var that = this;
    var page = 1;
    var limit = that.data.limit;
    var path = 'Index/index';
    var tid = that.data.tid;
    var data = {
      page: page,
      limit: limit,
      tid: tid,
    }
    if (app.globalData.openUserInfo == '') {
      var islogin = false;
    } else {
      var islogin = true;
    }
    ikz.request(path, data, islogin, (res) => {
      if (res.data.status == 'success') {
        var typeid = [];
        for (var i = 0; i < res.data.head_type_info.length; i++) {
          typeid[i] = res.data.head_type_info[i].id;
        }

        res.data.head_type_info.unshift({ head_type_id: 0, head_type_name: '推荐' });//给其分类前面添加一个 全部
        that.setData({
          head_info: res.data.head_info,//所有头条信息
          storageinfo: res.data.head_type_info, //存储数据
          currentinfo: res.data.head_type_info, //显示数据
          types: res.data.head_type_info,//全部分类
          advert_info: res.data.advert_info,//所有广告信息
          type_id: typeid,
          tid: tid,
          page: page,
        });
        // 隐藏导航栏加载框  
        wx.hideNavigationBarLoading();
        // 停止下拉动作  
        wx.stopPullDownRefresh();
        wx.hideLoading()
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        })
      }

    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    wx.showLoading({
      title: '玩命加载中',
    })
    var that = this;
    var page = that.data.page + 1;
    var limit = that.data.limit;
    var tid = that.data.tid;
    var old_info = that.data.head_info;
    that.setData({
      page: page,
    })
    var path = 'Index/index';
    var data = {
      page: page,
      limit: limit,
      tid: tid,
    }
    if (app.globalData.openUserInfo == '') {
      var islogin = false;
    } else {
      var islogin = true;
    }
    ikz.request(path, data, islogin, (res) => {

      if (res.data.status == 'success') {
        var new_info = res.data.head_info;
        if (new_info.length) {
          var all_info = old_info.concat(new_info);
          var prodis = 'none';
        } else {
          var all_info = old_info;
          var prodis = 'block';
        }
        that.setData({
          head_info: all_info,//所有头条信息
          tid: tid,
          prodis: prodis,
        })
        // 隐藏导航栏加载框  
        wx.hideNavigationBarLoading();
        // 停止下拉动作  
        wx.stopPullDownRefresh();
        wx.hideLoading()
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        })
      }

    });
    wx.hideLoading();
  },
  getRandomColor() {
    let rgb = [];
    for (let i = 0; i < 3; ++i) {
      let color = Math.floor(Math.random() * 256).toString(16);
      color = color.length == 1 ? '0' + color : color;
      rgb.push(color);
    }
    return '#' + rgb.join('');
  },
})
